{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch.autograd import Variable\n",
    "import numpy as np\n",
    "\n",
    "from wavenet_modules import constant_pad_1d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Variable containing:\n",
      "(0 ,.,.) = \n",
      "   0   1   2   3   4   5   6   7\n",
      "   8   9  10  11  12  13  14  15\n",
      "  16  17  18  19  20  21  22  23\n",
      "[torch.FloatTensor of size 1x3x8]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "t = Variable(torch.linspace(0, 23, steps=24).view(1, 3, 8))\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "first transpose:  Variable containing:\n",
      "(0 ,.,.) = \n",
      "   0\n",
      "   1\n",
      "   2\n",
      "   3\n",
      "   4\n",
      "   5\n",
      "   6\n",
      "   7\n",
      "\n",
      "(1 ,.,.) = \n",
      "   8\n",
      "   9\n",
      "  10\n",
      "  11\n",
      "  12\n",
      "  13\n",
      "  14\n",
      "  15\n",
      "\n",
      "(2 ,.,.) = \n",
      "  16\n",
      "  17\n",
      "  18\n",
      "  19\n",
      "  20\n",
      "  21\n",
      "  22\n",
      "  23\n",
      "[torch.FloatTensor of size 3x8x1]\n",
      "\n",
      "view change:  Variable containing:\n",
      "(0 ,.,.) = \n",
      "   0   1\n",
      "   2   3\n",
      "   4   5\n",
      "   6   7\n",
      "\n",
      "(1 ,.,.) = \n",
      "   8   9\n",
      "  10  11\n",
      "  12  13\n",
      "  14  15\n",
      "\n",
      "(2 ,.,.) = \n",
      "  16  17\n",
      "  18  19\n",
      "  20  21\n",
      "  22  23\n",
      "[torch.FloatTensor of size 3x4x2]\n",
      "\n",
      "second transpose:  Variable containing:\n",
      "(0 ,.,.) = \n",
      "   0   2   4   6\n",
      "   8  10  12  14\n",
      "  16  18  20  22\n",
      "\n",
      "(1 ,.,.) = \n",
      "   1   3   5   7\n",
      "   9  11  13  15\n",
      "  17  19  21  23\n",
      "[torch.FloatTensor of size 2x3x4]\n",
      "\n",
      "Variable containing:\n",
      "(0 ,.,.) = \n",
      "   0   2   4   6\n",
      "   8  10  12  14\n",
      "  16  18  20  22\n",
      "\n",
      "(1 ,.,.) = \n",
      "   1   3   5   7\n",
      "   9  11  13  15\n",
      "  17  19  21  23\n",
      "[torch.FloatTensor of size 2x3x4]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def dilate(x, dilation):\n",
    "    [n, c, l] = x.size()\n",
    "    dilation_factor = dilation / n\n",
    "    if dilation == n:\n",
    "        return x\n",
    "\n",
    "    # zero padding for reshaping\n",
    "    new_l = int(np.ceil(l / dilation_factor) * dilation_factor)\n",
    "    if new_l != l:\n",
    "        l = new_l\n",
    "        x = constant_pad_1d(x, new_l, dimension=2, pad_start=True)\n",
    "\n",
    "    # reshape according to dilation\n",
    "    x = x.permute(1, 2, 0).contiguous()\n",
    "    print(\"first transpose: \", x)\n",
    "    \n",
    "    l = (l * n) // dilation\n",
    "    n = dilation\n",
    "    \n",
    "    x = x.view(c, l, n)\n",
    "    print(\"view change: \", x)\n",
    "    \n",
    "    x = x.permute(2, 0, 1)\n",
    "    #x = x.transpose(1, 2).transpose(0, 2).contiguous()\n",
    "    print(\"second transpose: \", x)\n",
    "\n",
    "    return x\n",
    "\n",
    "r = dilate(t, 2)\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "first transpose:  Variable containing:\n",
      "(0 ,.,.) = \n",
      "   0   1\n",
      "   2   3\n",
      "   4   5\n",
      "   6   7\n",
      "\n",
      "(1 ,.,.) = \n",
      "   8   9\n",
      "  10  11\n",
      "  12  13\n",
      "  14  15\n",
      "\n",
      "(2 ,.,.) = \n",
      "  16  17\n",
      "  18  19\n",
      "  20  21\n",
      "  22  23\n",
      "[torch.FloatTensor of size 3x4x2]\n",
      "\n",
      "view change:  Variable containing:\n",
      "(0 ,.,.) = \n",
      "   0   1   2   3\n",
      "   4   5   6   7\n",
      "\n",
      "(1 ,.,.) = \n",
      "   8   9  10  11\n",
      "  12  13  14  15\n",
      "\n",
      "(2 ,.,.) = \n",
      "  16  17  18  19\n",
      "  20  21  22  23\n",
      "[torch.FloatTensor of size 3x2x4]\n",
      "\n",
      "second transpose:  Variable containing:\n",
      "(0 ,.,.) = \n",
      "   0   4\n",
      "   8  12\n",
      "  16  20\n",
      "\n",
      "(1 ,.,.) = \n",
      "   1   5\n",
      "   9  13\n",
      "  17  21\n",
      "\n",
      "(2 ,.,.) = \n",
      "   2   6\n",
      "  10  14\n",
      "  18  22\n",
      "\n",
      "(3 ,.,.) = \n",
      "   3   7\n",
      "  11  15\n",
      "  19  23\n",
      "[torch.FloatTensor of size 4x3x2]\n",
      "\n",
      "Variable containing:\n",
      "(0 ,.,.) = \n",
      "   0   4\n",
      "   8  12\n",
      "  16  20\n",
      "\n",
      "(1 ,.,.) = \n",
      "   1   5\n",
      "   9  13\n",
      "  17  21\n",
      "\n",
      "(2 ,.,.) = \n",
      "   2   6\n",
      "  10  14\n",
      "  18  22\n",
      "\n",
      "(3 ,.,.) = \n",
      "   3   7\n",
      "  11  15\n",
      "  19  23\n",
      "[torch.FloatTensor of size 4x3x2]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "r2 = dilate(r, 4)\n",
    "print(r2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    ""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3.0
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}